//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////@

Log.print('service module: feredata');

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////@

Service.feredata = {};

Service.feredata.action = function(nodereq) {
	var skiniko = nodereq.skinikoGet(), sinedria, klen;

	nodereq.header('json');
	if (nodereq.isvoli()) return;

	sinedria = nodereq.sinedriaGet();
	sinedria.feredataClose();

	klen = skiniko.kinisiPlithos();
	if (nodereq.url.hasOwnProperty('freska')) {
		skiniko.sizitisiFeredata(nodereq);
		skiniko.rebelosFeredata(nodereq);
		skiniko.trapeziFeredata(nodereq);
		skiniko.sinedriaFeredata(nodereq);
		skiniko.pektisFeredata(nodereq);
		if (skiniko.kinisiPlithos() != klen)
			return Service.feredata.error(nodereq, 'ανιχνεύθηκαν νέες κινήσεις');

		nodereq.sinedria.floterSet(klen);
		nodereq.end();
		return;
	}

	sinedria.
	feredataSet(nodereq).
	enimerosi(skiniko);
}

Service.feredata.error = function(nodereq, msg) {
	nodereq.end('?:' + globals.json(msg));
}

// Θα υπολογιστεί κατά το στήσιμο των περιπόλων.

Service.feredata.timeout = 0;

Service.feredata.check = function(skiniko) {
	var tora = globals.tora();

	skiniko.sinedriaWalk(function(login, sinedria) {
		var feredata = sinedria.feredataGet();
		if (!feredata) return;
		if (tora - sinedria.pollGet() < Service.feredata.timeout) return;

//console.log('feredata timeout: ' + login);
		sinedria.feredataClose(feredata, '"=":0');
	});
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////@

Skiniko.prototype.sizitisiFeredata = function(nodereq) {
	nodereq.write('z:' + JSON.stringify(this.sizitisi) + ',');
	return this;
}

Skiniko.prototype.rebelosFeredata = function(nodereq) {
	var skiniko = this;

	nodereq.write('r:{');
	this.rebelosWalk(function(pektis, rebelos) {
		delete rebelos.skiniko;
		nodereq.write(globals.json(pektis) + ':' + JSON.stringify(rebelos) + ',');
		rebelos.skiniko = skiniko;
	});

	nodereq.write('},');
	return this;
}

Skiniko.prototype.trapeziFeredata = function(nodereq) {
	nodereq.write('t:{');
	this.trapeziWalk(function(kodikos, trapezi) {
		var proxiro = new Trapezi(trapezi).trapezi2object();
		proxiro.prosklisiWalk(function(kodikos, prosklisi) {
			if (prosklisi.apoGet() == nodereq.login) return;
			if (prosklisi.prosGet() == nodereq.login) return;
			proxiro.prosklisiDelete(kodikos);
		});

		nodereq.write(kodikos + ':' + JSON.stringify(proxiro) + ',');
	});

	nodereq.write('},');
	return this;
}

Skiniko.prototype.sinedriaFeredata = function(nodereq) {
	var skiniko = this;
	nodereq.write('s:{');
	this.sinedriaWalk(function(pektis, sinedria) {
		delete sinedria.skiniko;
		nodereq.write(globals.json(pektis) + ':' +
			JSON.stringify(new Sinedria(sinedria).sinedriaIncognito()) + ',');
		sinedria.skiniko = skiniko;
	});
	nodereq.write('},');
}

Skiniko.prototype.pektisFeredata = function(nodereq) {
	nodereq.write('p:{');
	this.pektisWalk(function(login, pektis) {
		nodereq.write(globals.json(login) + ':' + JSON.stringify(
			new Pektis(pektis).pektis2object().pektisProsarmogi(nodereq.login)
		) + ',');
	});
	nodereq.write('},');
}
